package com.etc2;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import java.io.IOException;
public class Invented {
    public static class InventedMapper extends Mapper<LongWritable, Text,Text,Text> {
        //产生<hwllo-文件名，1>
        @Override
        protected void map(LongWritable key, Text value,Mapper<LongWritable, Text, Text, Text>.Context context) throws IOException, InterruptedException {
            String[] split = value.toString().split("-");
            context.write(new Text(split[0]),new Text(split[1].replaceAll("\t","-->")));
        }
    }
    public static class InventedReducer extends Reducer<Text,Text,Text,Text>{
        //<hello,a.txt -->4> <hello,b.txt-->4> <hello,c.txt-->4>
        @Override
        protected void reduce(Text key, Iterable<Text> values, Reducer<Text, Text, Text, Text>.Context context) throws IOException, InterruptedException {
            //StringBuffer 是线程安全的，StringBuiler是非线程安全的，在不涉及线程安全下，StringBuiler更快
            StringBuilder sb = new StringBuilder();

            for (Text value : values){
               sb.append(value.toString()).append("\t");
            }
            context.write(key,new Text(sb.toString()));
        }
    }
    public static void main(String[] args) throws Exception {
        Configuration config = new Configuration();

        Job job = Job.getInstance(config);
        job.setJarByClass(Invented.class);

        job.setMapperClass(InventedMapper.class);
        job.setReducerClass(InventedReducer.class);

        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(Text.class);

        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(Text.class);

        FileInputFormat.setInputPaths(job,new Path("f:/all/a/cde/bb"));
        FileOutputFormat.setOutputPath(job,new Path("f:/all/a/cde/bbb"));

//        job.waitForCompletion(true);

       System.exit(job.waitForCompletion(true)?0 : 1);
    }
}
